/*
 * Copyright (c) 2019. Mikes. All rights reserved.
 *
 * You can contact me with sending email to bWlrZXNfMjAxMkAxNjMuY29t for more information!
 */

package com.climber.android.commonres.widget

import android.content.Context
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.View
import android.widget.*
import androidx.constraintlayout.widget.Guideline
import com.climber.android.commonres.helper.onClick
import com.climber.android.commonres.R

/**
 * Created by mikes on 2018/5/3.
 */
class CommonInputSMSCodeView : LinearLayout {
    private lateinit var tv_left: TextView
    private lateinit var iv_left: ImageView
    private lateinit var et_input_sms_code: EditText
    private lateinit var tv_send_sms_code : TextView

    private lateinit var leftLayout : FrameLayout

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init(attrs)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(attrs)
    }

    private fun init(attrs: AttributeSet) {
        View.inflate(context, R.layout.com_view_input_sms_code, this)

        orientation = VERTICAL

        leftLayout = findViewById(R.id.leftLayout)

        tv_left = findViewById(R.id.tv_left)
        iv_left = findViewById(R.id.iv_left)
        et_input_sms_code = findViewById(R.id.et_input_sms_code)
        tv_send_sms_code = findViewById(R.id.tv_send_sms_code)

        val guideLine = findViewById<Guideline>(R.id.guideLine)

        val a = context.obtainStyledAttributes(attrs,
            R.styleable.CommonInputContentView)

        a.getFloat(R.styleable.CommonInputContentView_ipvGuideLinePercent, 0.2f).run {
            guideLine.setGuidelinePercent(this)
        }

        a.getResourceId(R.styleable.CommonInputContentView_ipvLeftSrc, 0).run {
            if(this > 0){
                tv_left.visibility = View.GONE
                iv_left.setImageResource(this)
            } else{
                iv_left.visibility = View.GONE
            }
        }

        a.getString(R.styleable.CommonInputContentView_ipvLeftText).run {
            if (!TextUtils.isEmpty(this)) {
                tv_left.text = this
            } else{
                tv_left.text = "验证码"
            }
        }

        a.getString(R.styleable.CommonInputContentView_ipvPasswordHint).run {
            if (!TextUtils.isEmpty(this)) {
                et_input_sms_code.hint = this
            }
        }


        a.recycle()

        et_input_sms_code.addTextChangedListener(object : TextWatcher{
            override fun afterTextChanged(s: Editable?) {
                tv_send_sms_code.isEnabled = (s?.length ?: 0) > 0
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

        })
    }

    fun registerSendSMSCodeClick(handler : (v : android.view.View?)-> Unit){
        tv_send_sms_code.onClick(handler = handler)
    }

    fun getSendSMSCodeView() : TextView = tv_send_sms_code

    fun getInputContent() : String = et_input_sms_code.text.toString().trim()

    fun registerLeftViewClick(handler : (v : android.view.View?)-> Unit){
        leftLayout.onClick(handler = handler)
    }

    fun getLeftTextView() : TextView = tv_left

    fun updateLeftContent(content : String){
        tv_left.text = content
    }
}